package cn.anecansaitin.whimsy.client.block.renderer;

import cn.anecansaitin.whimsy.common.block.entity.RockAltarEntity;
import cn.anecansaitin.whimsy.common.register.ModItems;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;

public class RockAltarRenderer implements BlockEntityRenderer<RockAltarEntity> {
    public RockAltarRenderer(BlockEntityRendererProvider.Context context) {
    }

    @Override
    public void render(RockAltarEntity blockEntity, float artialTick, PoseStack poseStack, MultiBufferSource buffer, int packedLight, int packedOverlay) {
        ItemStack itemStack = blockEntity.itemHandler.getStackInSlot(0);

        if (itemStack.isEmpty()) {
            return;
        }

        Minecraft mc = Minecraft.getInstance();
        ItemRenderer itemRenderer = mc.getItemRenderer();
        BakedModel bakedmodel = itemRenderer.getModel(itemStack, blockEntity.getLevel(), null, 0);
        poseStack.pushPose();
        poseStack.translate(0.5, 0.5, 0.5);
        poseStack.scale(0.5f, 0.5f, 0.5f);
        poseStack.mulPose(Axis.YP.rotation(blockEntity.getSpin()));
        ItemDisplayContext context;

        if (itemStack.is(ModItems.ROCK_SCEPTRE.get())) {
            context = ItemDisplayContext.FIRST_PERSON_RIGHT_HAND;
            poseStack.translate(0, 0.2, 0);
            poseStack.mulPose(Axis.XN.rotationDegrees(45));
        } else {
            context = ItemDisplayContext.FIXED;
        }

        itemRenderer.render(itemStack, context, false, poseStack, buffer, packedLight, packedOverlay, bakedmodel);
        poseStack.popPose();
    }
}
